$(function() {
    const token = GetQueryString("token");
    // 默认第一页
    let page = 1;
    // 每页条数
    let pageSize = 5;
    ajaxList(page);

    function ajaxList(page) {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': "News|news_list", 'p': page, 'page_size': pageSize },
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status == 1) {
                    let html = '';

                    res.list.forEach(item => {
                        // // console.log(item);
                        let date = getDate(item.create_time);
                        html += `
                            <a href="view/headline_details.html?id=${item.id}&token=${token}" class="headlineItem">
                                <div class="headlineTop">
                                    <img src="${item.company_logo == '' ? 'img/user.jpg' : item.company_logo}" alt="${item.company_name}">
                                    <div class="headlineTopChild">
                                        <p>${item.company_name}</p>`
                        if (item.level_logo != '') {
                            html += `<img class="userLevel" src="${item.level_logo}" alt="${item.level_name}">`
                        }
                        html += `</div>
                                </div>
                                <div class="headlineText">
                                    ${item.title}
                                </div>
                                <div class="headlineImg">`
                        if (item.poster != '') {
                            html += `<img src="${item.poster}" alt="${item.company_name}">`
                        }
                        html += `</div>
                                <div class="headlineBottom">
                                    <p><span>${item.read_count}</span>人浏览</p>
                                    <p>${date}</p>
                                </div>
                            </a>
                            `;
                    })
                    if (res.list.length < pageSize) {
                        $('.notMore').css('display', 'flex');
                    }
                    $('.headlineLists').append(html);
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    }

    // 下拉加载更多
    $(window).scroll(function() {
        if ($(document).scrollTop() >= $(document).height() - $(window).height() - 1) {
            // console.log('我到底了');
            if ($('.notMore').css('display') != "flex") {
                page++;
                ajaxList(page);
            }
        }
    })

})